package com.googlecode.dgwt.client.dojo.data.format;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.googlecode.dgwt.client.dojo.Dojo;
import com.googlecode.dgwt.client.dojo.exception.ValidationException;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class ObjectFormatter extends BaseFormatter<Object> {

    private static ObjectFormatter formatter = GWT.create(ObjectFormatter.class);
    
    protected ObjectFormatter() {
    }
    
    @Override
    public Object unformatValue(String value) throws ValidationException {
        throw new ValidationException("Could not parse the value. Please use a specific ValueFormatter.");
    }

    @SuppressWarnings("unchecked")
    public static <T> ValueFormatter<T> get(){
        return (ValueFormatter<T>) formatter;
    }
    
    @SuppressWarnings("unchecked")
    public static <T> ValueFormatter<T> get(Class<T> type){
        
        if (Dojo.reflect.isAssignableFrom(type, Byte.class)) {
            return (ValueFormatter<T>) ByteFormatter.get();
        }
        
        if (Dojo.reflect.isAssignableFrom(type, Short.class)) {
            return (ValueFormatter<T>) ShortFormatter.get();
        }
        
        if (Dojo.reflect.isAssignableFrom(type, Integer.class)) {
            return (ValueFormatter<T>) IntegerFormatter.get();
        }
        
        if (Dojo.reflect.isAssignableFrom(type, Long.class)) {
            return (ValueFormatter<T>) LongFormatter.get();
        }
        
        if (Dojo.reflect.isAssignableFrom(type, Float.class)) {
            return (ValueFormatter<T>) FloatFormatter.get();
        }
        
        if (Dojo.reflect.isAssignableFrom(type, Double.class)) {
            return (ValueFormatter<T>) DoubleFormatter.get();
        }
        
        if (Dojo.reflect.isAssignableFrom(type, Character.class)) {
            return (ValueFormatter<T>) CharFormatter.get();
        }
        
        if (Dojo.reflect.isAssignableFrom(type, String.class)) {
            return (ValueFormatter<T>) StringFormatter.get();
        }
        
        if (Dojo.reflect.isAssignableFrom(type, Date.class)) {
            return (ValueFormatter<T>) DateFormatter.get();
        }

        return get();
    }
    
}
